//
//  WBNewFeatureView.swift
//  SinaWeibo
//
//  Created by 美食家 on 17/4/12.
//  Copyright © 2017年 不孤独的美食家. All rights reserved.
//

import UIKit

class WBNewFeatureView: UIView {
    
    
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var enterWeibo: UIButton!

    @IBOutlet weak var pageControl: UIPageControl!
    
    @IBAction func startWeibo() {
        removeFromSuperview()
    }
    class func WBNewFeatureView()-> WBNewFeatureView{
        
        let v = UINib.init(nibName: "WBNewFeatureView", bundle: nil)
        let nib = v.instantiate(withOwner: nil, options: nil)[0] as! WBNewFeatureView
        nib.frame = UIScreen.main.bounds
        return   nib
        
    }
    
    override func awakeFromNib() {
        
        let count = 4
        let rect  = UIScreen.main.bounds
        
        for n in 0..<count  {
            let imgName = "new_feature_\(n+1)"
            let iv = UIImageView(image: UIImage(named:imgName))
            iv.frame=rect.offsetBy(dx: CGFloat(n)*(rect.width), dy: 0)
            scrollView.addSubview(iv)
        }
        
        scrollView.contentSize = CGSize(width: CGFloat(count+1)*rect.width, height: rect.height)
        scrollView.bounces=false
        scrollView.showsVerticalScrollIndicator=false
        scrollView.showsHorizontalScrollIndicator=false
        scrollView.isPagingEnabled=true
        enterWeibo.isHidden=true
        scrollView.delegate=self
    }
}

extension WBNewFeatureView: UIScrollViewDelegate{

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let page=Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        if page == scrollView.subviews.count{
                removeFromSuperview()
        }
        
        enterWeibo.isHidden = (page != (scrollView.subviews.count-1))
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        enterWeibo.isHidden = true
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width+0.5)
        pageControl.currentPage=page
        pageControl.isHidden = (page == scrollView.subviews.count)
    }

}
